﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CMS.Core {
	public class Category {
		/// <summary>
		/// 缓存 Category 数据, 无需每次从数据源读取.
		/// </summary>
		private static List<Model.Category> _categories = null;

		static Category() {
			_categories = (List<Model.Category>)new Dao.Category().List();
		}

		/// <summary>
		/// 当前请求页面的 Category ID
		/// </summary>
		public static int CurrentID {
			get;
			set;
		}

		public static Model.Category GetModel(int categoryId) {
			return _categories.Find(delegate(Model.Category c) { return c.ID == categoryId; });
		}

		public static Model.Category GetModel(string alias) {
			return _categories.Find(delegate(Model.Category c) { return c.Alias == alias; });
		}

		/// <summary>
		/// 获取所有一级栏目项
		/// </summary>
		/// <returns></returns>
		public static List<Model.Category> GetMainItems() {
			return GetSubItems(0);
		}

		/// <summary>
		/// 获取子级栏目项
		/// </summary>
		/// <param name="parentId">对应的父栏目ID</param>
		/// <returns></returns>
		public static List<Model.Category> GetSubItems(int parentId) {
			return _categories.FindAll(delegate(Model.Category c) { return c.ParentID == parentId; });
		}

		/// <summary>
		/// 根据 Category ID 获取 Category Title
		/// </summary>
		/// <param name="categoryId"></param>
		/// <returns></returns>
		public static string GetCategoryTitle(int categoryId) {
			Model.Category model = _categories.Find(delegate(Model.Category c) { return c.ID == categoryId; });

			if (model != null)
				return model.Title;
			else
				return null;
		}
	}
}
